﻿$(document).ready(function () {
    Win8Desk.Init();
    Win8Desk.Resize();
    $(window).resize(function () { Win8Desk.Resize(); });
    if (document.addEventListener) {
        document.addEventListener("DOMMouseScroll", Win8Desk.Scroll, false);
    }
    document.onmousewheel = Win8Desk.Scroll;
});

var Win8Desk = {
    Theme: "lightblue",
    Init: function () {
        if (this.Theme == "lightblue") $("#main ul").before("<img class='bg' src='../static/frame/Win8Desk/theme/lightblue/images/main.jpg' style='position:absolute; z-index:0;' />");
        this.Menu.init();
        $(".win8-menu li").hover(function () { $(this).addClass("win8-menu-hover"); }, function () { $(this).removeClass("win8-menu-hover"); })
        .click(function () {
            $(".win8-menu li").removeClass("win8-menu-cur");
            $(this).addClass("win8-menu-cur");
            Win8Desk.Menu.select(this);
        });
        $("li.win8-icon-exit").hover(function () { $(this).addClass("win8-icon-exit-hover"); }, function () { $(this).removeClass("win8-icon-exit-hover"); })
        .click(function () {
            if (confirm('您确定要退出系统吗？')) {
                window.location = '../logout';
            }
        });
        $("li.win8-icon-setting").hover(function () { $(this).addClass("win8-icon-setting-hover"); }, function () { $(this).removeClass("win8-icon-setting-hover"); })
        .click(function () {
        	modifyPwdDialog();
        });
        $("div.win8-avatar").hover(function () { $(this).addClass("win8-avatar-hover"); }, function () { $(this).removeClass("win8-avatar-hover"); })
        .click(function () {
        	modifyPwdDialog();
        });
        //默认打开第一个一级菜单
        $(".win8-menu li:first").click();
    },
    Scroll: function (e) {
        e = e || event;
        var v = 0;
        if (e.wheelDelta) {
            v = e.wheelDelta;
        }
        else if (e.detail) {
            v = -e.detail;
        }
        var menu = v > 0 ? $(".win8-menu li.win8-menu-cur").prev() : $(".win8-menu li.win8-menu-cur").next();
        if (menu == null || typeof menu == undefined) {
            menu = v > 0 ? $(".win8-menu li:first") : $(".win8-menu li:last");
        }
        $(menu).click();
    },
    Resize: function () {
        var h = $(window).height() - $("#bar").height() - $("#task").height();
        $("#pedants div").height(h > 550 ? 250 : (h - 40) / 2);

        var w = $(window).width();
        $(window).width(w < 1024 ? 1024 : w);

        if (w < 1360) {
            $("#main ul").width(818);
        }
        else {
            $("#main ul").css("width", "60%");
        }

        h += 20;
        $("#main").height(h);
        if (this.Theme == "lightblue") $("#main img.bg").css({ height: h, width: w });
    },
    //菜单
    Menu:
    {
        _menus: [],  //二级菜单
        //初始化一级菜单
        init: function () {
            $.ajax({
                type: 'POST',
                url: '../sysWebMenu/queryAMenuList',
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                async: false,
                success: function (data) {
                	
                    if (data != null && data != undefined && data != "") {
                    	var m = [];
                        $.each(eval(data), function (i, n) {
                            m.push("<li id='" + n.id + "'>" + n.name + "</li>");

                            var mm = {};
                            mm.id = n.id;
                            mm.menu = [];
                            Win8Desk.Menu._menus.push(mm);
                        });
                        $(".win8-menu").html(m.join(""));
                    }
                },
                error: function (e) {
                    if (window.console && window.console.error) {
                        window.console.error("Win8 Desk >> \n  " + "Create Desk getAMenu Error" + e.statusText);
                    }
                }
            });
        },
        //选择一级菜单
        select: function (o) {
            var id = $(o).attr("id");
            var menu = this.read(id);
            if (menu == null || menu == "") {
                $.ajax(
                {
                    type: 'POST',
                    url: '../sysWebMenu/queryBMenuList',
                    data:{arg:id},
                    dataType: 'json',
                    async: false,
                    success: function (data) {
                        if (data != null && data != undefined && data != "") {
                            menu = [];
                            var color = ["#2d89ef", "#5ab40f", "#6550a7", "#dc552d", "#dd7633", "#a300ab", "#b02238", "#15add4", "#77bb30", "#0f962d"];
                            $.each(eval(data), function (i, n) {
                                var m = {};
                                m.id = n.id;
                                m.name = n.name;
                                m.url = '..'+n.linkUrl;
                                m.icon = "../static/frame/Win8Desk/theme/" + Win8Desk.Theme + "/images/" + (n.Icon == null || n.Icon.length == 0 ? "default.png" : n.Icon);
                                m.bgcolor = n.metroColor != null && n.metroColor.length > 0 ? "#" + n.metroColor : color[parseInt(n.id) % 10];
                                m.metro = n.metroSize == "Big" ? "win8-metro-big" : "win8-metro-small";
                                menu.push(m);
                            });
                            Win8Desk.Menu.cache(id, menu);
                        }
                    },
                    error: function (e) {
                        if (window.console && window.console.error) {
                            window.console.error("Win8 Desk >> \n  " + "Create Desk getBMenu Error" + e.statusText);
                        }
                    }
                });
            }
            this.display(menu);
        },
        //读取指定的一级菜单
        read: function (id) {
            var ret = null;
            $.each(Win8Desk.Menu._menus, function (i, n) {
                if (n.id == id) {
                    if (n.menu == null || n.menu == "") return false;
                    ret = [];
                    $.each(n.menu, function (i, n) {
                        ret.push(n);
                    });
                    return false;
                }
            });
            return ret;
        },
        //缓存菜单
        cache: function (id, menu) {
            $.each(Win8Desk.Menu._menus, function (i, n) {
                if (n.id == id) {
                    n.menu = menu;
                    return false;
                }
            });
        },
        //显示
        display: function (menu) {
            var html = [];
            $.each(menu, function (i, n) {
                html.push("<li class='" + n.metro + "' style='background-color:" + n.bgcolor + ";' id='" + n.id + "' name='" + n.name + "' url='" + n.url + "' icon='" + n.icon + "'><img src='" + n.icon + "'" + " /><div>" + n.name + "</div></li>");
            });
            if (menu.length == 0) {
                $.ajax({
                    url: "Sys/DockPanel/Win8/SysDockPanelDisplay.aspx",
                    async: false,
                    type: "GET",
                    contentType: "application/html",
                    success: function (data) {
                        $("#main ul").html(data);
                    },
                    error: function (x) {
                        console.log(x.responseText);
                    }
                });
                //$("#main ul").html("<iframe src='Sys/DockPanel/Win8/SysDockPanelDisplay.aspx' frameborder='0' style='width:1366px;height:400px;padding:0;'/>"); //html.join(""));
            }
            else {
                $("#main ul").html(html.join(""));
            }
            //$("#main ul").html(html.join(""));
            $("#main li").hover(function () { $(this).addClass("win8-main-hover"); }, function () { $(this).removeClass("win8-main-hover"); }).click(function () { Win8Desk.Task.open(this); });
        }
    },
    //任务栏
    Task:
    {
        _tasks: [],
        //打开的数量
        count: function () { return Win8Desk.Task._tasks.length; },
        //打开
        open: function (o) {
            var id = $(o).attr("id");
            var flag = false;
            $.each(Win8Desk.Task._tasks, function (i, n) {
                if (id == n.id) {
                    flag = true;
                    return false;
                }
            });
            if (!flag) {
                if (Win8Desk.Task._tasks.length >= 10) {
                    alert("您打开的页面太多了，请关闭不再使用的页面！");
                    return;
                }

                var t = {};
                t.id = id;
                t.name = $(o).attr("name");
                t.url = $(o).attr("url");
                t.icon = $(o).attr("icon");
                Win8Desk.Task._tasks.push(t);
            }
            this.display();
            this.activate(id);
        },
        //关闭
        close: function (id) {
            $.each(Win8Desk.Task._tasks, function (i, n) {
                if (id == n.id) {
                    Win8Desk.Task._tasks.splice(i, 1);
                    Win8Desk.Task.display();
                    Win8Desk.Page.close(id);
                    return false;
                }
            });
            var evt = window.event || arguments.callee.caller.arguments[0];
            
            if ($.support.msie) {
            	evt.cancelBubble = true;
            }
            else {
            	evt.stopPropagation();
            }
        },
        //激活
        activate: function (id) {
            var p = {};
            $.each(Win8Desk.Task._tasks, function (i, n) {
                if (id == n.id) {
                    p.id = n.id;
                    p.title = n.name;
                    p.url = n.url;
                    p.icon = n.icon;
                    return false;
                }
            });
            Win8Desk.Page.show(p);
        },
        display: function () {
            var html = [];
            $.each(Win8Desk.Task._tasks, function (i, n) {
                html.push("<li f='" + n.id + "'><img src=" + n.icon + " /><a href='javascript:void(0)' title='关闭' onclick='Win8Desk.Task.close(" + n.id + ")'>×</a><div>" + n.name + "</div></li>");
            });
            $("#task ul").html(html.join(""));
            $("#task li").hover(function () { $(this).addClass("win8-task-hover"); }, function () { $(this).removeClass("win8-task-hover"); }).click(function () { Win8Desk.Task.activate($(this).attr("f")); });
        }
    },
    //页面
    Page:
    {
        _x: 0,
        _y: 0,
        move: false,
        d: { id: "1", title: "", url: "", icon: "", drag: true },
        show: function (options) {
            if (options == undefined || options.id.length == 0 || options.title.length == 0 || options.url.length == 0) {
                alert("参数出错了！");
                return;
            }
            this.d = $.extend(this.d, options);

            //判断是否存在
            var id = "#box" + this.d.id;
            if ($(id).length > 0) {
                $(".win8-page-box").css("z-index", 999);
                $(id).slideDown().css("z-index", 9999);
                return;
            }

            var height = $(window).height();
            var width = $(window).width();
            var html = "<div id='box" + this.d.id + "' v='" + this.d.id + "' class='win8-page-box' style='width:" + width + "px;height:" + height + "px;top:0px;left:0px;z-index:9999'>"
                     + "<div class='win8-page-title'><img src='" + this.d.icon + "' /><font style='font-size:14px; font-weight:bold; color:#eee; cursor:default;'>" + this.d.title + "</font><ul><li class='win8-icon-close' title='关闭'></li><li class='win8-icon-normal' title='向下还原'></li><li class='win8-icon-max' style='display:none;' title='最大化'></li><li class='win8-icon-min' title='最小化'></li></ul></div>"
                     + "<div class='win8-page-content' style='width: " + width + "px;height:" + (height - 34) + "px;'>"
                     + "<iframe id='frm" + this.d.id + "' src='" + this.d.url + "' width='100%' height='" + (height - 34) + "px' scrolling='auto' frameborder='0' marginheight='0' marginwidth='0'></iframe>"
                     + "</div>"
                     + "</div>";
            $("body").append(html);
            $(id).slideDown();
            $("#box" + this.d.id + " .win8-icon-min").hover(function () { $(this).addClass("win8-icon-min-hover"); }, function () { $(this).removeClass("win8-icon-min-hover"); })
            .click(function () {
                var id = $(this).parents(".win8-page-box").attr("v");
                $("#box" + id).slideUp("slow");
            });
            $("#box" + this.d.id + " .win8-icon-close").hover(function () { $(this).addClass("win8-icon-close-hover"); }, function () { $(this).removeClass("win8-icon-close-hover"); })
            .click(function () {
                var id = $(this).parents(".win8-page-box").attr("v");
                Win8Desk.Task.close(id);
            });
            $("#box" + this.d.id + " .win8-icon-normal").hover(function () { $(this).addClass("win8-icon-normal-hover"); }, function () { $(this).removeClass("win8-icon-normal-hover"); })
            .click(function () {
                var id = $(this).parents(".win8-page-box").attr("v");
                $(this).hide();
                $("#box" + id + " .win8-icon-max").show();
                var width = $(window).width();
                var left = (width - 900) / 2;
                if (left < 0) left = 0;
                var height = $(window).height() - $("#task").height();
                var top = (height - 540) / 2;
                if (top < 0) top = 0;
                $("#box" + id).css({ width: 900, height: 540, top: top, left: left });
                $("#frm" + id).css({ width: 900, height: 540 - 28 });
                Win8Desk.Page.drag(id);
            });
            $("#box" + this.d.id + " .win8-icon-max").hover(function () { $(this).addClass("win8-icon-max-hover"); }, function () { $(this).removeClass("win8-icon-max-hover"); })
            .click(function () {
                var id = $(this).parents(".win8-page-box").attr("v");
                $(this).hide();
                $("#box" + id + " .win8-icon-normal").show();
                var height = $(window).height();
                var width = $(window).width();
                $("#box" + id).css({ width: width, height: height, top: 0, left: 0 });
                $("#frm" + id).css({ width: width, height: height - 34 });
                Win8Desk.Page.nodrag(id);
            });
        },
        drag: function (id) {
            $("#box" + id + " .win8-page-title").css({ cursor: "move" })
            .mousedown(function (e) {
                this.move = true;
                $(".win8-page-box").css("z-index", 999);
                $(this).parents(".win8-page-box").css("z-index", 9999);
                this._x = e.pageX - parseInt($(this).parents(".win8-page-box").css("left"));
                this._y = e.pageY - parseInt($(this).parents(".win8-page-box").css("top"));
            })
            .mousemove(function (e) { if (this.move) $(this).parents(".win8-page-box").css({ left: e.pageX - this._x, top: e.pageY - this._y }); })
            .mouseup(function (e) { this.move = false; });
        },
        nodrag: function (id) {
            $("#box" + id + " .win8-page-title").css({ cursor: "default" }).unbind("mousedown mousemove mouseup");
        },
        close: function (id) {
            $("#box" + id).hide(600).delay(1000, function () { $("#frm" + id).remove(); $(this).remove(); });
        },
        ie6: function () { return $.browser.msie && $.browser.version == "6.0"; }
    }
};
